/**
 * 新闻服务
 *
 * 处理新闻的CRUD操作和搜索功能
 */
import { api } from './api';
import { News, NewsCreate, NewsUpdate, PaginatedResponse, PaginationParams } from '../types';

export interface NewsListParams extends PaginationParams {
  keyword?: string;
  category?: string;
  published_only?: boolean;
  featured_only?: boolean;
}

export const newsService = {
  /**
   * 获取新闻列表
   * @param params 查询参数
   * @returns 分页的新闻列表
   */
  getNews: async (params: NewsListParams = {}): Promise<PaginatedResponse<News>> => {
    const queryParams = new URLSearchParams();

    if (params.page) queryParams.append('page', String(params.page));
    if (params.size) queryParams.append('size', String(params.size));
    if (params.keyword) queryParams.append('keyword', params.keyword);
    if (params.category) queryParams.append('category', params.category);
    if (params.published_only !== undefined) queryParams.append('published_only', String(params.published_only));
    if (params.featured_only !== undefined) queryParams.append('featured_only', String(params.featured_only));

    const url = `/api/news?${queryParams.toString()}`;
    return api.get<PaginatedResponse<News>>(url);
  },

  /**
   * 获取新闻详情
   * @param id 新闻ID
   * @returns 新闻详情
   */
  getNewsDetail: async (id: number): Promise<News> => {
    return api.get<News>(`/api/news/${id}`);
  },

  /**
   * 创建新闻
   * @param data 新闻数据
   * @returns 创建的新闻
   */
  createNews: async (data: NewsCreate): Promise<News> => {
    return api.post<News>('/api/news', data);
  },

  /**
   * 更新新闻
   * @param id 新闻ID
   * @param data 更新数据
   * @returns 更新后的新闻
   */
  updateNews: async (id: number, data: NewsUpdate): Promise<News> => {
    return api.put<News>(`/api/news/${id}`, data);
  },

  /**
   * 删除新闻
   * @param id 新闻ID
   * @returns 删除结果
   */
  deleteNews: async (id: number): Promise<{ message: string }> => {
    return api.delete<{ message: string }>(`/api/news/${id}`);
  },

  /**
   * 获取最新新闻（用于前台显示）
   * @param limit 数量限制
   * @returns 最新新闻列表
   */
  getLatestNews: async (limit: number = 6): Promise<News[]> => {
    const queryParams = new URLSearchParams();
    queryParams.append('published_only', 'true');
    queryParams.append('size', String(limit));

    const url = `/api/news?${queryParams.toString()}`;
    const response = await api.get<PaginatedResponse<News>>(url);
    return response.items;
  },

  /**
   * 获取置顶新闻
   * @returns 置顶新闻列表
   */
  getFeaturedNews: async (): Promise<News[]> => {
    const queryParams = new URLSearchParams();
    queryParams.append('published_only', 'true');
    queryParams.append('featured_only', 'true');
    queryParams.append('size', '3');

    const url = `/api/news?${queryParams.toString()}`;
    const response = await api.get<PaginatedResponse<News>>(url);
    return response.items;
  },

  /**
   * 搜索新闻
   * @param keyword 搜索关键词
   * @param params 其他查询参数
   * @returns 搜索结果
   */
  searchNews: async (keyword: string, params: Omit<NewsListParams, 'keyword'> = {}): Promise<PaginatedResponse<News>> => {
    const queryParams = new URLSearchParams();

    if (params.page) queryParams.append('page', String(params.page));
    if (params.size) queryParams.append('size', String(params.size));
    if (keyword) queryParams.append('keyword', keyword);
    if (params.category) queryParams.append('category', params.category);
    queryParams.append('published_only', 'true');
    if (params.featured_only !== undefined) queryParams.append('featured_only', String(params.featured_only));

    const url = `/api/news?${queryParams.toString()}`;
    return api.get<PaginatedResponse<News>>(url);
  },
};